import 'package:abc/controller/account_recovery_controller.dart';
import 'package:abc/controller/settings_controller.dart';
import 'package:abc/page/account_recovery_page.dart';
import 'package:abc/page/change_password_page.dart';
import 'package:abc/page/clear_cache_page.dart'; // 添加清理缓存页面导入
import 'package:abc/page/profile_page.dart';
import 'package:abc/view/CustomAppBar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SettingsPage extends GetView<SettingsController> {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (!Get.isRegistered<SettingsController>()) {
      Get.lazyPut(() => SettingsController());
    }
    return Scaffold(
      backgroundColor: const Color(0xFF0E0F26),
      appBar: CustomAppBar(
        title: "设置",
        onBack: () {
          Get.back();
        },
      ),

      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(15.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // --- 分组一：个人资料 (独立卡片) ---
              _buildSingleCard(
                icon: 'setting_user',
                title: '个人资料',
                onTap: () {
                  Get.to(() => const ProfilePage());
                },
                isFirst: true,
                isLast: true,
              ),

              const SizedBox(height: 10), // 分组间隔
              // --- 分组二：设置账号密码 & 账号找回 (合并卡片) ---
              _buildGroupCard([
                // 设置账号密码 (位于分组顶部，有底部分隔线，顶部圆角)
                SettingsItem(
                  icon: 'setting_key',
                  title: '设置账号密码',
                  onTap: () {
                
                    Get.to(() => const ChangePasswordPage());
                  },
                  showDivider: false,
                  borderRadius: const BorderRadius.vertical(
                    top: Radius.circular(10),
                  ),
                ),
                // 账号找回 (位于分组底部，无底部分隔线，底部圆角)
                SettingsItem(
                  icon: 'setting_phone',
                  title: '账号找回',
                  onTap: () {
                  
                    Get.to(() => const AccountRecoveryPage());
                  },
                  showDivider: false,
                  borderRadius: const BorderRadius.vertical(
                    bottom: Radius.circular(10),
                  ),
                ),
              ]),

              const SizedBox(height: 10), // 分组间隔
              // --- 分组三：绑定邀请码 (独立卡片) ---
              _buildSingleCard(
                icon: 'setting_invite',
                title: '绑定邀请码',
                onTap: () => print('打开绑定邀请码弹窗'),
              ),

              const SizedBox(height: 10), // 分组间隔
              // --- 分组四：清理缓存 (独立卡片) ---
              _buildSingleCard(
                icon: 'setting_rocket',
                title: '清理缓存',
                onTap: () {
                  Get.to(() => const ClearCachePage());
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 构建独立卡片的辅助方法
  Widget _buildSingleCard({
    required String icon,
    required String title,
    required VoidCallback onTap,
    bool isFirst = false,
    bool isLast = false,
  }) {
    return Container(
      decoration: BoxDecoration(
        color: const Color(0x33959595),
        borderRadius: BorderRadius.circular(10),
      ),
      child: SettingsItem(
        icon: icon,
        title: title,
        onTap: onTap,
        showDivider: false,
        borderRadius: BorderRadius.circular(10), // 整体圆角
      ),
    );
  }

  // 构建合并卡片的辅助方法
  Widget _buildGroupCard(List<Widget> children) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: const Color(0x33959595),
      ),
      child: Column(children: children),
    );
  }
}

class SettingsItem extends StatelessWidget {
  final String icon; // 左侧图标
  final String title; // 标题
  final VoidCallback onTap; // 点击回调
  final bool showDivider; // 是否显示底部分隔线
  final BorderRadius borderRadius; // 列表项的圆角

  const SettingsItem({
    super.key,
    required this.icon,
    required this.title,
    required this.onTap,
    this.showDivider = true,
    this.borderRadius = BorderRadius.zero, // 默认无圆角
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 70,
        decoration: BoxDecoration(
          // color: const Color(0x33959595), // 卡片背景色
          borderRadius: borderRadius,
        ),
        child: Column(
          children: [
            Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: Row(
                  children: [
                    Image.asset(
                      'assets/images/${icon}.png',
                      width: 24,
                      height: 24,
                    ),
                    const SizedBox(width: 10),
                    // 标题
                    Text(
                      title,
                      style: const TextStyle(color: Colors.white, fontSize: 16),
                    ),
                    const Spacer(),
                    // 右侧箭头
                    const Icon(
                      Icons.arrow_forward_ios,
                      color: Colors.white54,
                      size: 16,
                    ),
                  ],
                ),
              ),
            ),
            // 底部分隔线
            if (showDivider)
              Padding(
                padding: const EdgeInsets.only(left: 15.0),
                child: Container(
                  height: 0.5,
                  color: Colors.white.withOpacity(0.1),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
